package com.maijinjie.admin.content.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.maijinjie.common.action.dwz.DwzUIBaseAction;
import com.maijinjie.common.action.dwz.bean.DwzStatusBean;
import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.admin.content.bean.PictureItemBean;
import com.maijinjie.admin.content.service.PictureItemService;
import com.maijinjie.admin.content.field.PictureItemFields;
import com.maijinjie.common.session.SessionContext;
import com.maijinjie.common.util.JsonUtil;
import com.maijinjie.common.util.PoiExcelUtil;

/**
 * PictureItemAction
 * @version 1.0
 * @author 罗正杰
 */
@Controller
@RequestMapping("/admin/content/pictureItem")
public class PictureItemAction extends DwzUIBaseAction{
	/** 用户服务接口 */
	@Resource
	PictureItemService pictureItemService;
	/** 列表页 */
	private String listPage = "/admin/sys/pictureItemList";
	/** 编辑页 */
	private String editPage = "/admin/sys/pictureItemEdit";
	/**
	 * 查询数据
	 * @param pageNum
	 * @param numPerPage
	 * @param pagination
	 * @param model
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public String list(Integer pageNum, Integer numPerPage,
			Pagination<PictureItemBean> pagination,Model model) throws Exception{
		pagination.setPage(pageNum);
		pagination.setRows(numPerPage);
		model.addAttribute("pagination",pictureItemService.list(pagination));
		return listPage;
	}
	/**
	 * 进入新增页
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addGET() throws Exception{
		return editPage;
	}
	/**
	 * 保存新增数据
	 * @param bean
	 * @param rel
	 * @param req
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void addPOST(PictureItemBean bean, String rel, HttpServletRequest req, HttpServletResponse res) throws Exception{
		StatusBean<?> status = pictureItemService.save(bean, SessionContext.getRealName(req), SessionContext.getUserId(req));
		if(status.isSuccess()){
			writeSuccessAndRefresh(status.getMessage(), rel, res);
		} else {
			writeError(status.getMessage(), res);
		}
	}
	/**
	 * 进入编辑页
	 * @param id
	 * @param model
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String editGET(Integer id, Model model) throws Exception{
		PictureItemBean bean = pictureItemService.getBeanById(id);
		model.addAttribute("bean", bean);
		return editPage;
	}
	/**
	 * 保存编辑数据
	 * @param bean
	 * @param rel
	 * @param model
	 * @param req
	 * @param res
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public void editPOST(PictureItemBean bean, String rel, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception{
		StatusBean<?> status = pictureItemService.update(bean, SessionContext.getRealName(req), SessionContext.getUserId(req));
		if(status.isSuccess()){
			writeSuccessAndRefresh(status.getMessage(), rel, res);
		} else {
			writeError(status.getMessage(), res);
		}
	}
	/**
	 * 删除数据
	 * @param id
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/del")
	public void del(Integer id,String parentId,String rel, HttpServletResponse res) throws Exception{
		StatusBean<?> status = pictureItemService.deleteById(id);
		if(status.isSuccess()){
			writeSuccessAndRefresh(status.getMessage(), rel, res);
		} else {
			writeError(status.getMessage(), res);
		}
	}
	/**
	 * 批量删除
	 * @param ids
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/batDel")
	public void batDel(String ids, HttpServletResponse res) throws Exception{
		StatusBean<?> status = pictureItemService.batchDelete(ids);
		if (status.isSuccess()) {
			writeSuccess(status.getMessage(), res);
		} else {
			writeError(status.getMessage(), res);
		}
	}
	
	/**
	 * 导出功能
	 * @param ids
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/exp")
	public void export(HttpServletResponse res) throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		List<PictureItemBean> list = pictureItemService.list(params);
		Workbook workbook = PoiExcelUtil.createSimpleExportWorkbook(
				PictureItemFields.values(), PictureItemFields.values(), list,PictureItemBean.class,"yyyy-MM-dd HH:mm:ss");
		this.writeDownloadExcelFile(PictureItemBean.class.getName()+".xlsx", workbook, res);
	}
}

